package com.inspur.sc.springboot.controller;

import com.inspur.sc.springboot.utils.HadoopUtil;
import com.inspur.sc.springboot.utils.ResponseUtil;
import com.inspur.sc.springboot.vo.ResponseVO;
import com.inspur.sc.springboot.vo.UploadFileVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@RestController
@RequestMapping("api/hdfs")
public class HadoopController {

    @Autowired
    private HadoopUtil hadoopUtil;

    /**
     * 文件上传
     * @param request
     * @param dst
     * @return
     * @throws IOException
     */
    @PostMapping("upload")
    public ResponseEntity<ResponseVO> upload(HttpServletRequest request,
                                             @RequestParam(value = "dst") String dst) throws IOException {
        MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
        List<MultipartFile> files = multiRequest.getFiles("files");
        if(!files.isEmpty()){
            for (MultipartFile file:files) {
                String fileName=file.getOriginalFilename();
                hadoopUtil.hdfsUpload(file.getInputStream(),dst+"/"+fileName,true);
            }
            return ResponseUtil.success("上传成功!",true);
        }
        return ResponseUtil.success("上传失败!",false);
    }

    /**
     * 文件下载
     * @param response
     * @param dst
     * @param fileName
     * @return
     * @throws IOException
     */
    @GetMapping("download")
    public ResponseEntity<ResponseVO> download(HttpServletResponse response,
                                               @RequestParam(value = "dst") String dst,
                                               @RequestParam(value = "fileName") String fileName) throws IOException {
        response.setContentType("application/force-download");// 设置强制下载不打开
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
        byte[] buffer = new byte[10240];
        InputStream fis = hadoopUtil.getFile(dst);
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(fis);
            OutputStream os = response.getOutputStream();
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    /**
     * 删除文件/目录
     * @param dst
     * @return
     */
    @PostMapping("delete")
    public ResponseEntity<ResponseVO> delete(@RequestParam(value = "dst") String dst){
        boolean isDel=hadoopUtil.delete(dst,true);
        if(isDel){
            return ResponseUtil.success("删除成功！",isDel);
        }
        return ResponseUtil.success("删除失败！",isDel);
    }

    /**
     * 创建目录
     * @param dir
     * @return
     */
    @PostMapping("mkdir")
    public ResponseEntity<ResponseVO> mkdir(@RequestParam(value = "dir") String dir){
        boolean is=hadoopUtil.mkdir(dir);
        if(is){
            return ResponseUtil.success("["+dir+"],目录创建成功！",is);
        }
        return ResponseUtil.success("["+dir+"],目录创建失败！",is);
    }

    /**
     * 获取文件列表
     * @param dst
     * @return
     */
    @GetMapping("getFiles")
    public ResponseEntity<ResponseVO> getFiles(@RequestParam(value = "dst") String dst){
        List<UploadFileVO> fileVOS=hadoopUtil.getFiles(dst);
        return ResponseUtil.success(fileVOS);
    }
}
